"""
Copyright (c) 2024 lowyyh
command-line-three-piece-chess is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
import random


def pd(j, x, y):
    if not j[x][y]:
        return True
    else:
        return False


def will_win(play, I, q):
    for i in range(3):
        if len(I[i]) == 2:
            for i2 in range(3):
                if i2 in I[i]:
                    pass
                else:

                    if (not (i2 in play[i])) and (not (i2 in I[i])):
                        # print("1横")
                        return i, i2
    # 纵向判断
    for i in range(3):
        if i in I[0] and i in I[1]:
            if (not (i in play[2])) and (not (i in I[2])):
                # print("1竖")
                return 2, i
        elif i in I[2] and i in I[1]:
            if (not (i in play[0])) and (not (i in I[0])):
                # print("2竖")
                return 0, i
        elif i in I[2] and i in I[0]:
            if (not (i in play[1])) and (not (i in I[1])):
                # print("3竖")
                return 1, i
    # 斜向判断
    if 0 in I[0] and 1 in I[1]:
        if (not (2 in play[2])) and (not (2 in I[2])):
            # print("1斜")
            return 2, 2
    elif (0 in I[0] and 2 in I[2]) or (2 in I[0] and 0 in I[2]) or (0 in I[2] and 2 in I[0]) or (
            2 in I[2] and 0 in I[0]):
        if (not (1 in play[1])) and (not (1 in I[1])):
            # print("2斜")
            return 1, 1
    elif 3 in I[0] and 1 in I[1]:
        if (not (2 in play[0])) and (not (2 in I[0])):
            # print("3斜")
            return 2, 0
    elif 0 in I[2] and 1 in I[1]:
        if (not (2 in play[0])) and (not (2 in I[0])):
            # print("4斜")
            return 0, 2
    elif 2 in I[2] and 1 in I[1]:
        if (not (0 in play[0])) and (not (0 in I[0])):
            # print("5斜")
            return 0, 0

    # 横向判断
    for i in range(3):
        if len(play[i]) == 2:
            for i2 in range(3):
                if i2 in play[i]:
                    pass
                else:
                    if (not (i2 in play[i])) and (not (i2 in I[i])):
                        # print("横")
                        return i, i2
    # 纵向判断
    for i in range(3):
        if i in play[0] and i in play[1]:
            if (not (i in play[2])) and (not (i in I[2])):
                # print("竖")
                return 2, i
        elif i in play[2] and i in play[1]:
            if (not (i in play[0])) and (not (i in I[0])):
                # print("竖2")
                return 0, i
        elif i in play[2] and i in play[0]:
            if (not (i in play[1])) and (not (i in I[1])):
                # print("竖3")
                return 1, i
    # 斜向判断
    if 0 in play[0] and 1 in play[1]:
        if (not (2 in play[2])) and (not (2 in I[2])):
            # print("斜")
            return 2, 2
    elif (0 in play[0] and 2 in play[2]) or (2 in play[0] and 0 in play[2]) or (0 in play[2] and 2 in play[0]) or (
            2 in play[2] and 0 in play[0]):
        if (not (1 in play[1])) and (not (1 in I[1])):
            # print("斜2")
            return 1, 1
    elif 2 in play[0] and 1 in play[1]:
        if (not (0 in play[2])) and (not (0 in I[2])):
            # print("斜3")
            return 2, 0
    elif 0 in play[2] and 1 in play[1]:
        if (not (2 in play[0])) and (not (2 in I[0])):
            # print("斜4")
            return 0, 2
    elif 2 in play[2] and 1 in play[1]:
        if (not (0 in play[0])) and (not (0 in I[0])):
            # print("斜5")
            return 0, 0
    if (not (0 in play[0])) or (not (2 in play[0])) or (not (0 in play[2])) or (not (2 in play(2))):
        # print("角2")
        for i in [[0, 0], [0, 2], [2, 0], [2, 2]]:
            if (not (i[1] in play[i[0]])) and (not (i[1] in I[i[0]])):
                return i[0], i[1]
    else:
        while True:
            x = random.randint(0, 2)
            y = random.randint(0, 2)
            if pd(q, x, y):
                return x, y


def mast_win(q, n):
    print("电脑下棋")
    if not n:
        # print("run")
        while True:
            if not q[1][1]:
                # print("先")
                q[1][1] = "#"
            else:
                # print("角")
                r = random.choices([[0, 0], [0, 2], [2, 0], [2, 2]])[0]
                q[r[0]][r[1]] = "#"
            return
    else:
        play = []
        I = []
        for i in range(3):
            tmp = []
            for i2 in range(3):
                if q[i][i2] == "*":
                    tmp.append(i2)
            play.append(tmp)
        for i in range(3):
            tmp = []
            for i2 in range(3):
                if q[i][i2] == "#":
                    tmp.append(i2)
            I.append(tmp)
        x, y = will_win(play, I, q)
        q[x][y] = "#"
